package com.fenghuajueli.libbasecoreui.adapter;

import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.fenghuajueli.libbasecoreui.databinding.ItemFeedbackTypeBinding;

import java.util.List;

public class FeedbackTypeAdapter extends RecyclerView.Adapter<FeedbackTypeAdapter.Holder> {

    private List<Pair<String, Integer>> mList;
    private FeedbackTypeItemListener listener;

    public void setItemListener(FeedbackTypeItemListener _listener){
        listener = _listener;
    }

    public FeedbackTypeAdapter(List<Pair<String, Integer>> list) {
        mList = list;
    }

    public void resetData(List<Pair<String, Integer>> list){
        mList = list;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public FeedbackTypeAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemFeedbackTypeBinding binding = ItemFeedbackTypeBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull FeedbackTypeAdapter.Holder holder, int position) {
        holder.binding.tvType.setText(mList.get(position).first);
        holder.itemView.setOnClickListener(v->{
            if (listener != null) {
                listener.onItemClick(mList.get(position).second);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    static class Holder extends RecyclerView.ViewHolder {

        public ItemFeedbackTypeBinding binding;

        public Holder(ItemFeedbackTypeBinding _binding) {
            super(_binding.getRoot());
            binding = _binding;
        }
    }

    public interface FeedbackTypeItemListener{
        void onItemClick(int feedbackType);
    }

}
